<?php

namespace Project\ScrumBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormError;

class SprintType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder
                ->add('date', 'date')
                ->add('pts_difficulte', 'text')
                ->add('refGroupe', 'entity', array(
                    'class' => 'Project\ScrumBundle\Entity\Groupe',
                    'property' => 'nom',
                    'label' => 'Groupe : '))
        ;

        $pointsValidator = function(FormEvent $event) {
                    $form = $event->getForm();
                    $pts_difficulteField = $form->get('pts_difficulte')->getData();
                    if (!(intval($pts_difficulteField) > 0 && intval($pts_difficulteField) <= 100)) {
                        $form['pts_difficulte']->addError(new FormError("compris entre 1 et 100"));
                    }
                };

        // ajout du validateur
        $builder->addEventListener(FormEvents::POST_BIND, $pointsValidator);
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Project\ScrumBundle\Entity\Sprint'
        ));
    }

    public function getName() {
        return 'project_scrumbundle_sprinttype';
    }

}
